/*!
 * @file modeWorld.cpp
 * @brief Ciało modeWorld.hh
 */

#include "modes/world/modeWorld.hh"
using namespace std;

/*--------------------------------------------------------------------------*/

modeWorld::modeWorld() :
	modeBase(MA_ON, MK_NORMAL, MD_NORMAL, "world") {

	this->config = NULL;
	this->screen = new texture(pawirr::graphic->resX, pawirr::graphic->resY);
}

/*--------------------------------------------------------------------------*/

void modeWorld::init() {

	this->_area = new areaMenager();
	this->_skybox = new skyboxMenager();
	this->_water = new waterMenager();

	this->godraysShader = new shader("godrays");

	this->godraysShader->addShader("pawirr/shaders/godrays.v", ST_V);
	this->godraysShader->addShader("pawirr/shaders/godrays.f", ST_F);

	this->godraysShader->addTexture("firstPass", this->_skybox->getGodraysTexture());
	this->godraysShader->addTexture("screen", this->screen);

	pawirr::graphic->_env->addEnvUniforms(this->godraysShader);

	this->godraysShader->linkProgram();
}

/*--------------------------------------------------------------------------*/

void modeWorld::render3d() {
	/*
	 * EFEKT: odbicia w wodzie
	 */
	this->rotateRender(rra_flip);

	pawirr::graphic->setRenderTarget(this->_water->getReflectionTexture());
	pawirr::graphic->clearFrame(pawirr::graphic->_env->skyColor2);

	this->_skybox->draw(skybox_dm_all);
	this->_area->draw(area_dm_lowpoly);

	pawirr::graphic->setRenderTarget(NULL);

	this->rotateRender(rra_normal);
	/*
	 * koniec odbić w wodzie
	 */
	/*------------------------------------------*/
	/*
	 * EFEKT: godrays
	 */
	pawirr::graphic->setRenderTarget(this->_skybox->getGodraysTexture());

	color bl = color(0.0, 0.0, 0.0, 1.0);
	pawirr::graphic->clearFrame(bl);

	this->_skybox->draw(skybox_dm_sun);
	this->_area->draw(area_dm_noshader);
	//this->_water->draw(water_dm_godrays);

	pawirr::graphic->setRenderTarget(NULL);

	//this->_skybox->getGodraysTexture()->copyColorBuffer();

	/*
	 * koniec godrays
	 */

	/*
	 * normalny teren
	 */
	pawirr::graphic->clearFrame(pawirr::graphic->_env->skyColor2);
	this->_skybox->draw(skybox_dm_all);
	this->_area->draw(area_dm_all);

	/*
	 * łapanie normalniego terenu do tekstury
	 */
	this->_water->getUnderwaterTexture()->copyColorBuffer();
	/*------------------------------------------*/
	/*
	 * malowanie wody
	 */
	this->_water->draw(water_dm_all);

	this->screen->copyColorBuffer();


	point3f aaa = this->_skybox->getSunWindowCoords();

	if(aaa.z < 1.0f && aaa.x > 0.0f && aaa.x < pawirr::graphic->resX
			&& aaa.y > 0.0f && aaa.y < pawirr::graphic->resY){

		this->godraysShader->bind();
		GLuint var_location;

		if (var_location = glGetUniformLocation(this->godraysShader->getProgram(),
				"lightPositionOnScreen")) {
			glUniform3f(var_location, aaa.x, aaa.y, aaa.z);
		}

		pawirr::graphic->_pri->putRectangle(point2f(0, 0), point2f(pawirr::graphic->resX, pawirr::graphic->resY),
				color_white);

		this->godraysShader->unbind();
	}

	//pawirr::graphic->_pri->putRectangle(point2f(0,0), point2f(aaa.x,aaa.y), color_white);
}

/*--------------------------------------------------------------------------*/

void modeWorld::rotateRender(rotateRenderArg arg) {

	if (arg == rra_normal) {
		glCullFace(GL_BACK);
		glDisable(GL_CLIP_PLANE0);
		glPopMatrix();

		pawirr::graphic->_env->cameraPos.y *= -1;
		pawirr::graphic->_env->lightDiffuseSource.y *= -1;
	} else if (arg == rra_flip) {
		glPushMatrix();
		glScalef(1.0f, -1.0f, 1.0f);

		glCullFace(GL_FRONT);
		double plane[4] = { 0.0, 1.0, 0.0, 0.0 };

		if (pawirr::graphic->_env->cameraPos.y < 0.0f) {
			plane[1] = -1.0;
		}

		glEnable(GL_CLIP_PLANE0);
		glClipPlane(GL_CLIP_PLANE0, plane);

		pawirr::graphic->_env->cameraPos.y *= -1;
		pawirr::graphic->_env->lightDiffuseSource.y *= -1;
	}
}

/*--------------------------------------------------------------------------*/

void modeWorld::render2d() {

	//pawirr::graphic->_pri->putRectangle(point2f(0, 0), point2f(200, 180),
	//		color_white, this->_skybox->getGodraysTexture());

	stringstream ss;
	ss << "(" << pawirr::graphic->_env->cameraPos.x << "; "
			<< pawirr::graphic->_env->cameraPos.y << "; "
			<< pawirr::graphic->_env->cameraPos.z << ")";

	pawirr::temp->fontDefault->renderToScreen(point2f(20, 500), color(1, 0, 0),
			ss.str());
}

/*--------------------------------------------------------------------------*/

void modeWorld::calculate() {

}

/*--------------------------------------------------------------------------*/

void modeWorld::input() {

	point3f prevCamPos = pawirr::graphic->_env->cameraPos;

	if (pawirr::keyboard->isPressed(k_space)) {
		this->_area->obj->position.y = 100.0f;
	}
	if (pawirr::keyboard->isPressedC(k_up)) {
		pawirr::graphic->_env->cameraPos.x
				-= pawirr::graphic->_env->cameraVec.x * 1.0f;
		pawirr::graphic->_env->cameraPos.y
				-= pawirr::graphic->_env->cameraVec.y * 1.0f;
		pawirr::graphic->_env->cameraPos.z
				-= pawirr::graphic->_env->cameraVec.z * 1.0f;
	}
	if (pawirr::keyboard->isPressedC(k_down)) {
		pawirr::graphic->_env->cameraPos.x
				+= pawirr::graphic->_env->cameraVec.x * 1.0f;
		pawirr::graphic->_env->cameraPos.y
				+= pawirr::graphic->_env->cameraVec.y * 1.0f;
		pawirr::graphic->_env->cameraPos.z
				+= pawirr::graphic->_env->cameraVec.z * 1.0f;
	}

	if (pawirr::keyboard->isPressedC(k_b)) {
		this->_area->obj->rotation.x += 0.01;
	}
	if (pawirr::keyboard->isPressedC(k_n)) {
		this->_area->obj->rotation.y += 0.01;
	}
	if (pawirr::keyboard->isPressedC(k_m)) {
		this->_area->obj->rotation.z += 0.01;
	}

	if (!this->_area->checkPos(pawirr::graphic->_env->cameraPos)) {
		pawirr::graphic->_env->cameraPos = prevCamPos;
	}

	if (pawirr::keyboard->isPressed(k_e)) {
		log::crash("Naciśnięto zły klawisz");
	}
}

/*--------------------------------------------------------------------------*/

void modeWorld::loadMap(std::string path) {

	pawirr::graphic->loadingScreen("Wczytawnie mapy...");
	this->config = new inifile(path + "config.ini");

	//cout << this->config->getString("area", "rTex",
	//		"pawirr/textures/replaceme.png") << endl;
	this->_area->loadMap(path);
}

/*--------------------------------------------------------------------------*/
